package com.example.yuejianapp14.Activity.Activity;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Model.TieziModel;
import com.example.yuejianapp14.R;

public class ReleaseActivity extends AppCompatActivity {
    private ImageView back;
    private ImageButton release_pic;
    private Button release_tiezi;
    private SharedPreferences sp;
    private EditText content;
    private String path=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release);
        initViews();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        release_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CharSequence[] items = {"相册,拍照"};
                new AlertDialog.Builder(ReleaseActivity.this).setTitle("选择获取头像方式").setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which==0){
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("image/*");
                            startActivityForResult(intent,1);
                        }
                    }
                }).create().show();
            }
        });
        release_tiezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TieziModel tieziModel = new TieziModel();
                tieziModel.Releasetiezi(sp.getString("uid",""),content.getText().toString(),path,ReleasetieziListener);

            }
        });
    }
    public void initViews() {
        back = (ImageView) findViewById(R.id.back);
        release_pic = (ImageButton)findViewById(R.id.release_pic);
        release_tiezi = (Button)findViewById(R.id.release_tiezi);
        sp=getSharedPreferences("uid",MODE_PRIVATE);
        content = (EditText)findViewById(R.id.tiezi_content);
    }
    ListListener<Success> ReleasetieziListener=new ListListener<Success>() {
        @Override
        public void onResponse(Success a) {
            if(a.getSuccess().equals("1")){
                Toast.makeText(ReleaseActivity.this, "发布成功",Toast.LENGTH_SHORT).show();
                finish();

            }
            else {
                Toast.makeText(ReleaseActivity.this, "发布失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ReleaseActivity.this, "fail",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (data!=null){
                    Uri uri = data.getData();
                    release_pic.setImageURI(uri);
                    ContentResolver cr = this.getContentResolver();
                    Cursor c = cr.query(uri, null, null, null, null);
                    c.moveToFirst();
                    //这是获取的图片保存在sdcard中的位置
                    path = c.getString(c.getColumnIndex("_data"));
                    //Toast.makeText(RegActivity.this, path, Toast.LENGTH_SHORT).show();
                }else {
                    return;
                }
                break;
        }
    }

}
